<template>
<div>
  <el-dialog
    :title="title"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="inputForm" ref="inputForm" v-loading="loading" :class="method==='view'?'readonly':''"  :disabled="method==='view'"
             label-width="120px">
      <el-row  :gutter="15">
        <el-col :span="12">
            <el-form-item label="姓名" prop="name"
                :rules="[
                  {required: true, message:'姓名不能为空', trigger:'blur'}
                 ]">
              <el-input v-model="inputForm.name" placeholder="请填写姓名"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="部门" prop="depart.id"
                :rules="[
                 ]">
          <SelectTree
                      ref="depart"
                      :props="{
                          value: 'id',             // ID字段名
                          label: 'name',         // 显示名称
                          children: 'childNodes'    // 子级字段名
                        }"

                      url="/lora/user/jfLoraDepart/treeData"
                      :value="inputForm.depart.id"
                      :clearable="true"
                      :accordion="true"
                      @getValue="(value) => {inputForm.depart.id=value}"/>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="工号" prop="code"
                :rules="[
                 ]">
              <el-input v-model="inputForm.code" placeholder="请填写工号"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="工牌号码" prop="rfid"
                :rules="[
                 ]">
              <el-input v-model="inputForm.rfid" placeholder="请填写工牌号码"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="性别" prop="sex"
                :rules="[
                 ]">
                    <el-radio-group v-model="inputForm.sex">
                        <el-radio v-for="item in $dictUtils.getDictList('sex')" :label="item.value" :key="item.id">{{item.label}}</el-radio>
                    </el-radio-group>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="手机号" prop="phone"
                :rules="[
                  { min: 11, message: '最小长度是11个字符', trigger: 'blur' },
                  {validator: validator.isTel, trigger:'blur'}
                 ]">
              <el-input v-model="inputForm.phone" placeholder="请填写手机号"  maxlength="11"  minlength="11"   ></el-input>
           </el-form-item>
        </el-col>
<!--        <el-col :span="12">
            <el-form-item label="身份证" prop="identity"
                :rules="[
                  { min: 15, message: '最小长度是15个字符', trigger: 'blur' },
                  {validator: validator.isCardId, trigger:'blur'}
                 ]">
              <el-input v-model="inputForm.identity" placeholder="请填写身份证"  maxlength="18"  minlength="15"   ></el-input>
           </el-form-item>
        </el-col> -->
        </el-row>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">关闭</el-button>
      <el-button type="primary" v-if="method != 'view'" @click="doSubmit()" v-noMoreClick>确定</el-button>
    </span>
  </el-dialog>
</div>
</template>

<script>
  import SelectTree from '@/components/treeSelect/treeSelect.vue'
  export default {
    data () {
      return {
        title: '',
        method: '',
        visible: false,
        loading: false,
        inputForm: {
          id: '',
          name: '',
          userType: '',
          depart: {
            id: ''
          },
          code: '',
          rfid: '',
          sex: '',
          phone: '',
          identity: ''
        }
      }
    },
    components: {
      SelectTree
    },
    methods: {
      init (method, id) {
        this.method = method
        this.inputForm.id = id
        if (method === 'add') {
          this.title = `新建员工`
        } else if (method === 'edit') {
          this.title = '修改员工'
        } else if (method === 'view') {
          this.title = '查看员工'
        }
        this.visible = true
        this.loading = false
        this.$nextTick(() => {
          this.$refs.inputForm.resetFields()
          if (method === 'edit' || method === 'view') { // 修改或者查看
            this.loading = true
            this.$http({
              url: `/lora/user/jfLoraUser/queryById?id=${this.inputForm.id}`,
              method: 'get'
            }).then(({data}) => {
              this.inputForm = this.recover(this.inputForm, data.jfLoraUser)
              this.loading = false
            })
          }
        })
      },
      // 表单提交
      doSubmit () {
        this.inputForm.userType = '1'
        this.$refs['inputForm'].validate((valid) => {
          if (valid) {
            this.loading = true
            this.$http({
              url: `/lora/user/jfLoraUser/save`,
              method: 'post',
              data: this.inputForm
            }).then(({data}) => {
              if (data && data.success) {
                this.visible = false
                this.$message.success(data.msg)
                this.$emit('refreshDataList')
              } else if(data && data.code === -2) {
                this.$confirm(data.msg+', 将此工牌绑定到当前员工吗?', '提示', {
                  confirmButtonText: '确定',
                  cancelButtonText: '取消',
                  type: 'warning'
                }).then(() => {
                  this.doForceSubmit()
                }).catch(() => {

                });
              }
              this.loading = false
            })
          }
        })
      },
      // 表单提交
      doForceSubmit () {
        this.inputForm.userType = '1'
        this.$refs['inputForm'].validate((valid) => {
          if (valid) {
            this.loading = true
            this.$http({
              url: `/lora/user/jfLoraUser/forceSave`,
              method: 'post',
              data: this.inputForm
            }).then(({data}) => {
              if (data && data.success) {
                this.visible = false
                this.$message.success(data.msg)
                this.$emit('refreshDataList')
              }
              this.loading = false
            })
          }
        })
      }
    }
  }
</script>
